using System;
using BillsDomain;
using System.Globalization;
using System.Collections.Generic;

namespace BillsPresentation
{
    public class AddPaymentPresenter
    {
        /// <summary>
        /// The view
        /// </summary>
        IAddPaymentView view;

        /// <summary>
        /// Gets or sets the controller.
        /// </summary>
        /// <value>The controller.</value>
        private IScheduledPaymentController controller { get; set; }

        /// <summary>
        /// Initializes a new instance of the AddPaymentPresenter class.
        /// </summary>
        /// <param name="view"></param>
        public AddPaymentPresenter(IAddPaymentView view, IScheduledPaymentController controller)
        {
            this.view = view;
            this.controller = controller;
            this.SubscribeViewToEvents();
        }

        /// <summary>
        /// Subscribes the view to events.
        /// </summary>
        public void SubscribeViewToEvents()
        {
            this.view.Load += OnLoad;
            this.view.FormClose += OnFormClosing;
            this.view.SaveClicked += OnSaveClicked;
        }

        /// <summary>
        /// Called when the save button is clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void OnSaveClicked(object sender, EventArgs e)
        {
            decimal amount;
            if (!decimal.TryParse(this.view.Amount, out amount))
            {
                //TODO: Show error
                amount = 0;
            }

            var newScheduledPayment = new ScheduledPayment();
            newScheduledPayment.Amount = amount;
            newScheduledPayment.FirstDate = this.view.StartDate;
            newScheduledPayment.Payee = this.view.PayeeLookup.SelectedItem as string;
            newScheduledPayment.PaymentRecurrence = this.view.RecurrenceLookup.SelectedItem as Recurrence;
            newScheduledPayment.Id = this.view.AddMode ? -1 : this.controller.CurrentScheduledPayment.Id;
            newScheduledPayment.PaymentType = this.view.TypeLookup.SelectedItem as PaymentType;
            newScheduledPayment.IncludeInProjection = this.view.IncludeInProjection;
            this.controller.UpdateListWith(newScheduledPayment);
        }

        /// <summary>
        /// Loads the view on model.
        /// </summary>
        private void LoadViewOnModel()
        {
            if (!this.view.AddMode)
            {
                this.view.PayeeLookup.SelectedItem = this.controller.CurrentScheduledPayment.Payee;
                this.view.Amount = this.controller.CurrentScheduledPayment.Amount.ToString(CultureInfo.CurrentCulture);
                this.view.RecurrenceLookup.SelectedItem = this.controller.CurrentScheduledPayment.PaymentRecurrence;
                this.view.StartDate = this.controller.CurrentScheduledPayment.FirstDate;
                this.view.TypeLookup.SelectedItem = this.controller.CurrentScheduledPayment.PaymentType;
                this.view.IncludeInProjection = this.controller.CurrentScheduledPayment.IncludeInProjection;
            }
            else
            {
                this.view.PayeeLookup.SelectedItem = String.Empty;
                this.view.Amount = String.Empty;
                this.view.RecurrenceLookup.SelectedItem = Recurrence.None;
                this.view.StartDate = DateTime.Now;
                this.view.TypeLookup.SelectedItem = PaymentType.Debit;
                this.view.IncludeInProjection = true;
            }
        }

        /// <summary>
        /// Called when the form loads.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void OnLoad(object sender, EventArgs e)
        {
            this.view.RecurrenceLookup.Clear();
            this.view.RecurrenceLookup.Add(Recurrence.None);
            this.view.RecurrenceLookup.Add(Recurrence.Weekly);
            this.view.RecurrenceLookup.Add(Recurrence.Biweekly);
            this.view.RecurrenceLookup.Add(Recurrence.Monthly);
            this.view.RecurrenceLookup.Add(Recurrence.Bimonthly);

            this.view.TypeLookup.Clear();
            this.view.TypeLookup.Add(PaymentType.Debit);
            this.view.TypeLookup.Add(PaymentType.Credit);

            List<string> payees = new List<string>();
            foreach (ScheduledPayment payment in this.controller.ScheduledPayments)
            {
                if (!payees.Contains(payment.Payee))
                {
                    payees.Add(payment.Payee);
                }
            }

            payees.Sort();
            this.view.PayeeLookup.Clear();
            foreach (string payee in payees)
            {
                this.view.PayeeLookup.Add(payee);
            }

            LoadViewOnModel();
        }

        /// <summary>
        /// Called when the form is closing.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void OnFormClosing(object sender, EventArgs e)
        {
            this.controller.SaveData();
        }
    }
}
